/**
 * 打开文件上传对话框
 * @param dialog_title 对话框标题
 * @param callback 回调方法，参数有（当前dialog对象，选择的文件数组，你设置的extra_params）
 * @param extra_params 额外参数，object
 * @param multi 是否可以多选
 * @param filetype 文件类型，image,video,audio,file
 * @param app  应用名，CMF的应用名
 */
function openUploadDialog(dialog_title, callback, extra_params, multi, filetype, app) {
    multi = multi ? 1 : 0;
    filetype = filetype ? filetype : 'image';
    app = app ? app : 'admin';
    var params = '&multi=' + multi + '&filetype=' + filetype + '&app=' + app;

    layer.open({
        type: 2,
        area: ['600px', '350px'], //宽高
        content: '/admin/Asset/webuploader?' + params,
        btn: ['确认'],
        yes: function (index, layero) {
//                    var body = layer.getChildFrame('body', index);
            var iframewindow = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
//                    console.log(body.html()); //得到iframe页的body内容
//                    body.find('input').val('Hi，我是从父页来的');

            if (typeof callback == 'function') {
//                var iframewindow = this.iframe.contentWindow;
                var files = iframewindow.get_selected_files();
                console.log(files);
                if (files && files.length > 0) {
                    callback.apply(this, [this, files, extra_params]);
                } else {
                    return false;
                }
            }
            layer.close(index);
        }
    });

}
/**
 * 单个图片上传
 * @param dialog_title 上传对话框标题
 * @param input_selector 图片容器
 * @param extra_params 额外参数，object
 * @param app  应用名,CMF的应用名
 */
function uploadOneImage(dialog_title, input_selector, extra_params, app) {
    openUploadDialog(dialog_title, function (dialog, files) {
        $(input_selector).val(files[0].filepath);
        $(input_selector + '-preview').attr('src', files[0].preview_url);

        $(input_selector + '-name').val(files[0].name);
        $(input_selector + '-name-text').text(files[0].name);

    }, extra_params, 0, 'image', app);
}
/**
 * 多图上传
 * @param dialog_title 上传对话框标题
 * @param container_selector 图片容器
 * @param item_tpl_wrapper_id 单个图片html模板容器id
 * @param extra_params 额外参数，object
 * @param app  应用名,CMF 的应用名
 */
function uploadMultiImage(dialog_title, container_selector, item_tpl_wrapper_id, extra_params, app) {
    openUploadDialog(dialog_title, function (dialog, files) {
        var tpl  = $('#' + item_tpl_wrapper_id).html();
        var html = '';
        $.each(files, function (i, item) {
            var itemtpl = tpl;
            itemtpl     = itemtpl.replace(/\{id\}/g, item.id);
            itemtpl     = itemtpl.replace(/\{url\}/g, item.url);
            itemtpl     = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
            itemtpl     = itemtpl.replace(/\{filepath\}/g, item.filepath);
            itemtpl     = itemtpl.replace(/\{name\}/g, item.name);
            html += itemtpl;
        });
        $(container_selector).append(html);

    }, extra_params, 1, 'image', app);
}
/**
 * 查看图片对话框
 * @param img 图片地址
 */
function imagePreviewDialog(img) {
        layer.photos({
            photos: {
                "title": "", //相册标题
                "id": 'image_preview', //相册id
                "start": 0, //初始显示的图片序号，默认0
                "data": [   //相册包含的图片，数组格式
                    {
                        "alt": "",
                        "pid": 666, //图片id
                        "src": img, //原图地址
                        "thumb": img //缩略图地址
                    }
                ]
            } //格式见API文档手册页
            , anim: 5, //0-6的选择，指定弹出图片动画类型，默认随机
            shadeClose: true,
            // skin: 'layui-layer-nobg',
            shade: [0.5, '#000000'],
            shadeClose: true,
        });
}
/**
 * 删除
 * @type type
 */
$(document).on('click', '.btn_delete', function () {
    var that = $(this);
    myajax_data(that);
});
/**
 更新
 */
$(document).on('click', '.btn_update', function () {
    var that = $(this);
    myajax_data(that);
});
/**
 * ajax的数据准备
 * @param {type} that dom对象
 * @param {type} $success  成功回调
 * @returns {undefined}
 */
function myajax_data(that, $success) {
    var url = that.data('url');
    var id = that.data('id');
    console.log('btn_delete');

    var $options = {};
    $options.url = url;
    if (id) {
        $options.id = id;
    }
    if ($success) {  //成功回调
        $options.success = $success;
    }
    var confirm_content = that.data('confirm_content') ? that.data('confirm_content') : '您是否确定要执行这个操作？';
    layer.confirm(confirm_content, {icon: 3, title: '提示'}, function (index, layero) {
        myajax($options);
    });

}
/**
 *  ajax操作
 * @param {type} $options
 * @returns {undefined}
 */
function myajax($options) {
    console.log($options);
    var args = $.extend({
        type: 'post',
        dataType: 'json',
        url: 'delete',
        data: {},
        success: function (data) {
            console.log('success');
            console.log(data);
            if (data.code == '1') {
                console.log('if');
                layer.alert(data.msg, function (index) {
                    console.log(index);
                    _refresh(data);
                    layer.close(index);
                });
            } else {
                console.log('else');
                layer.alert(data.msg);
            }
        },
        error: function () {
        }
    }, $options);
    console.log(args);
    $.ajax(args);
//        layer.close(index);
}
/**
 * 重新刷新
 * @param {type} data
 * @returns {undefined}
 */
function _refresh(data) {
    console.log(data);
    if (data.url) {
        //返回带跳转地址
        window.location.href = data.url;
    } else {
        if (data.code == 1) {
            //刷新当前页
            var location = window.location;
            var href = location.pathname + location.search;
            console.log(href);
            location.href = href;
        }
    }
}